package com.older.wptsb.controller.community;


import com.older.wptsb.domain.dto.AlertLogDTO;
import com.older.wptsb.domain.dto.CommunityContactDTO;
import com.older.wptsb.domain.dto.CommunityDTO;
import com.older.wptsb.domain.dto.UpdateCommunityDTO;
import com.older.wptsb.domain.po.RequireRole;
import com.older.wptsb.domain.po.UserRole;
import com.older.wptsb.domain.vo.AlertLogVO;
import com.older.wptsb.domain.vo.CommunityContactVO;
import com.older.wptsb.domain.vo.MyCommunityVO;
import com.older.wptsb.service.CommunityService;
import com.older.wptsb.util.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/community")
public class CommunityController {
    @Resource
    private CommunityService communityService;
    @RequireRole({UserRole.ROLE_COMMUNITY})
    @GetMapping("/current")
    public Result<String> getCurrentCommunity(){
       String communityId = communityService.getCurrentCommunity();
        return Result.success(communityId);
    }

    @RequireRole({UserRole.ROLE_COMMUNITY})
    @GetMapping("/get")
    public Result<MyCommunityVO> getCommunity(){
        MyCommunityVO myCommunityVO = communityService.getMyCommunity();
        return Result.success(myCommunityVO);
    }

    @RequireRole({UserRole.ROLE_COMMUNITY})
    @PutMapping("/update")
    public Result<String> updateCommunity(@RequestBody UpdateCommunityDTO updateCommunityDTO){
        communityService.updateCommunity1(updateCommunityDTO);
        return Result.success("更新成功");
    }

    @RequireRole({UserRole.ROLE_COMMUNITY})
    @GetMapping("/getCommunityUser")
    public Result<List<CommunityContactVO>> getCommunityUser(){
        List<CommunityContactVO> CommunityContactVOList = communityService.getCommunityUser();
        return Result.success(CommunityContactVOList);
    }
    @RequireRole({UserRole.ROLE_COMMUNITY})
    @PostMapping("/addCommunityUser")
    public Result<String> addCommunityUser(@RequestBody CommunityContactDTO communityContactDTO){
        communityService.addCommunityUser(communityContactDTO);
        return Result.success("添加成功");
    }
    @RequireRole({UserRole.ROLE_COMMUNITY})
    @PutMapping("/updateCommunityUser/{id}")
    public Result<String> updateCommunityUser(@PathVariable Integer id, @RequestBody CommunityContactDTO communityContactDTO){
        communityService.updateCommunityUser(id,communityContactDTO);
        return Result.success("更新成功");
    }

    @RequireRole({UserRole.ROLE_COMMUNITY})
    @DeleteMapping("/deleteCommunityUser/{id}")
    public Result<String> deleteCommunityUser(@PathVariable Integer id){
        System.out.println("id:" + id);
        communityService.deleteCommunityUser(id);
        System.out.println("删除成功");
        return Result.success("删除成功");
    }


    @RequireRole({UserRole.ROLE_COMMUNITY})
    @GetMapping("/alertlog")
    public Result<List<AlertLogVO>> getAlertLog(){
        List<AlertLogVO> alertLogVOList = communityService.getAlertLog();
        return Result.success(alertLogVOList);
    }
    @RequireRole({UserRole.ROLE_COMMUNITY})
    @GetMapping("/alertlog/{id}")
    public Result<AlertLogVO> getAlertLog(@PathVariable Integer id){
        AlertLogVO alertLogVO = communityService.getAlertLogById(id);
        return Result.success(alertLogVO);
    }
    @RequireRole({UserRole.ROLE_COMMUNITY})
    @PutMapping("/updateAlertlog")
    public Result<String> updateAlertlog(@RequestBody AlertLogDTO alertLogDTO){
        communityService.updateAlertlog(alertLogDTO);
        return Result.success("更新成功");
    }
}
